
#include "setting_unbind.h"
#include "pt_ipc.h"
#include "sys_font.h"
#include "pt_fs.h"

static ui_setting_unbind_t  ui_set_unbind;

static  char * icon_unbind[1] = 
{
    "Warning.png",
};

static int setting_unbind_cancel_clicked_event(void)
{
    debug_d("unbind cancel\n");

    ui_set_unbind.timeout  = 6;
    if(ui_set_unbind.reset_time){
        lv_timer_pause(ui_set_unbind.reset_time);
    }

    page_pop();
    root_gesture_enable();

    return 0;
}

static void setting_unbind_cancel_event_callback(lv_event_t* event)
{
    if (event->code == LV_EVENT_CLICKED) {
        setting_unbind_cancel_clicked_event();
    }
}

static int setting_unbind_yes_clicked_event(void)
{
    if(ui_set_unbind.timeout > 0)
        return -1;
    // //send unbind command
    // uint8_t flag = 1;
    // pt_unbind_flag_set(flag);
    // pt_ipc_send_msg(IPC_MSG_UNBIND_STAT,(char*)(&flag),1);
    // debug_d("unbind yes\n");

    return 0;
}

static void setting_unbind_yes_event_callback(lv_event_t* event)
{
    if (event->code == LV_EVENT_CLICKED) {
        setting_unbind_yes_clicked_event();
    }
}

static int setting_unbind_status_update(void)
{
    char txt_buff[256];

    if(ui_set_unbind.timeout <= 0)
        return 0;

    ui_set_unbind.timeout--;

    memset(txt_buff,0,sizeof(txt_buff));

    if(ui_set_unbind.timeout == 0)
    {
        snprintf(txt_buff,256,"删除");
        lv_obj_set_style_bg_opa(ui_set_unbind.bg_yes,LV_OPA_100,LV_PART_MAIN);    
        lv_obj_set_style_text_opa(ui_set_unbind.lab_yes,LV_OPA_100,LV_PART_MAIN);
    }
    else
    {
        snprintf(txt_buff,256,"删除(%ds)",ui_set_unbind.timeout);
    }

    lv_label_set_text(ui_set_unbind.lab_yes,txt_buff);

    return 0;
}

void setting_unbind_timer_callback(struct _lv_timer_t * t)
{
    setting_unbind_status_update();
}

int ui_setting_unbind_create(lv_obj_t *superview)
{
    char txt_buff[256];

    ui_set_unbind.timeout = 6;

    ui_set_unbind.img_tip = lv_img_create(superview);
    lv_img_set_src(ui_set_unbind.img_tip,pt_fs_get(icon_unbind[0]));
    lv_obj_set_pos(ui_set_unbind.img_tip,200,22);
    //**********************************************************************************************
    ui_set_unbind.lab_tip = lv_label_create(superview);
    lv_obj_set_pos(ui_set_unbind.lab_tip,24,124);
    lv_obj_set_size(ui_set_unbind.lab_tip,432,108);
    lv_obj_set_style_text_align(ui_set_unbind.lab_tip,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_set_unbind.lab_tip,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_set_unbind.lab_tip,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_set_unbind.lab_tip, lv_font_get(20), LV_STATE_DEFAULT);
    lv_label_set_text( ui_set_unbind.lab_tip, "取消绑定后，设备将会与您当前登陆的美居账号解除绑定并清除设备储存在服务器和本地的数据。（该操作不可逆）");
    //**********************************************************************************************
    //cancel btn

    ui_set_unbind.bg_cancel = lv_obj_create(superview);

    lv_obj_set_pos(ui_set_unbind.bg_cancel,60,246);
    lv_obj_set_size(ui_set_unbind.bg_cancel,170,50);

	lv_obj_set_style_radius(ui_set_unbind.bg_cancel, 25, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_unbind.bg_cancel, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_unbind.bg_cancel, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui_set_unbind.bg_cancel, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui_set_unbind.bg_cancel, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui_set_unbind.bg_cancel, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(ui_set_unbind.bg_cancel, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui_set_unbind.bg_cancel, 0,0);
    
    #if 0
    lv_obj_set_style_bg_opa(ui_set_unbind.bg_cancel,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_set_unbind.bg_cancel,LV_OPA_100,0);
    lv_obj_set_style_bg_color(ui_set_unbind.bg_cancel,lv_color_make(0x26,0x7a,0xff),0);
    #endif 

    lv_obj_add_event_cb(ui_set_unbind.bg_cancel, setting_unbind_cancel_event_callback, LV_EVENT_ALL, NULL);
    //**********************************************************************************************
    ui_set_unbind.lab_cancel = lv_label_create(ui_set_unbind.bg_cancel);
    lv_obj_set_pos(ui_set_unbind.lab_cancel,0,5);
    lv_obj_set_size(ui_set_unbind.lab_cancel,170,30);
    lv_obj_set_style_text_align(ui_set_unbind.lab_cancel,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_set_unbind.lab_cancel,LV_OPA_100,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_set_unbind.lab_cancel,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_set_unbind.lab_cancel, lv_font_get(28), LV_STATE_DEFAULT);

    lv_label_set_text(ui_set_unbind.lab_cancel,"取消");
    //**********************************************************************************************
    //**********************************************************************************************
    //yes btn

    ui_set_unbind.bg_yes = lv_obj_create(superview);

    lv_obj_set_pos(ui_set_unbind.bg_yes,275,246);
    lv_obj_set_size(ui_set_unbind.bg_yes,170,50);

	lv_obj_set_style_radius(ui_set_unbind.bg_yes, 25, LV_PART_MAIN);
	lv_obj_set_style_border_width(ui_set_unbind.bg_yes, 0, LV_PART_MAIN);
	lv_obj_set_style_border_color(ui_set_unbind.bg_yes, lv_color_make(0x00, 0x00, 0x00), LV_PART_MAIN);
	lv_obj_set_style_border_opa(ui_set_unbind.bg_yes, LV_OPA_0, LV_PART_MAIN);

	lv_obj_set_style_outline_width(ui_set_unbind.bg_yes, 0, LV_PART_MAIN);
	lv_obj_set_style_shadow_width(ui_set_unbind.bg_yes, 0, LV_PART_MAIN);
	lv_obj_set_scrollbar_mode(ui_set_unbind.bg_yes, LV_SCROLLBAR_MODE_OFF);
	lv_obj_set_style_pad_all(ui_set_unbind.bg_yes, 0,0);
    
    #if 0
    lv_obj_set_style_bg_opa(ui_set_unbind.bg_yes,LV_OPA_0,0);
    #else
    lv_obj_set_style_bg_opa(ui_set_unbind.bg_yes,LV_OPA_30,0);
    lv_obj_set_style_bg_color(ui_set_unbind.bg_yes,lv_color_make(0x26,0x7a,0xff),0);
    #endif 

    lv_obj_add_event_cb(ui_set_unbind.bg_yes, setting_unbind_yes_event_callback, LV_EVENT_ALL, NULL);
    //**********************************************************************************************
    ui_set_unbind.lab_yes = lv_label_create(ui_set_unbind.bg_yes);
    lv_obj_set_pos(ui_set_unbind.lab_yes,0,5);
    lv_obj_set_size(ui_set_unbind.lab_yes,170,30);
    lv_obj_set_style_text_align(ui_set_unbind.lab_yes,LV_TEXT_ALIGN_CENTER,0);
    lv_obj_set_style_text_opa(ui_set_unbind.lab_yes,LV_OPA_30,LV_PART_MAIN);
    lv_obj_set_style_text_color(ui_set_unbind.lab_yes,lv_color_make(0xff,0xff,0xff),LV_PART_MAIN); 
    lv_obj_set_style_text_font(ui_set_unbind.lab_yes, lv_font_get(28), LV_STATE_DEFAULT);

    memset(txt_buff,0,sizeof(txt_buff));
    snprintf(txt_buff,256,"删除(%ds)",ui_set_unbind.timeout);

    lv_label_set_text(ui_set_unbind.lab_yes,txt_buff);
    //**********************************************************************************************
    if(!ui_set_unbind.reset_time){
        ui_set_unbind.reset_time = lv_timer_create(setting_unbind_timer_callback,1000,NULL);
    }else {
        lv_timer_reset(ui_set_unbind.reset_time);
    }
    //**********************************************************************************************

    return 0;
}


int ui_setting_unbind_destroy(lv_obj_t *superview)
{
    ui_set_unbind.timeout = 6;

    if(ui_set_unbind.img_tip){
        lv_obj_del(ui_set_unbind.img_tip);
        ui_set_unbind.img_tip = NULL;
    }
    if(ui_set_unbind.lab_tip){
        lv_obj_del(ui_set_unbind.lab_tip);
        ui_set_unbind.lab_tip = NULL;
    }
    if(ui_set_unbind.bg_cancel){
        lv_obj_del(ui_set_unbind.bg_cancel);
        ui_set_unbind.bg_cancel = NULL;
        ui_set_unbind.lab_cancel = NULL;
    }
    if(ui_set_unbind.bg_yes){
        lv_obj_del(ui_set_unbind.bg_yes);
        ui_set_unbind.bg_yes = NULL;
        ui_set_unbind.lab_yes = NULL;
    }
    if(ui_set_unbind.reset_time){
        lv_timer_del(ui_set_unbind.reset_time);
        ui_set_unbind.reset_time = NULL;
    }

    return 0;
}

static void setting_unbind_on_custom_attr_config(page_base_t* self)
{


}
static void setting_unbind_on_view_load(page_base_t* self)
{


}
static void setting_unbind_on_view_did_load(page_base_t* self)
{
    ui_setting_unbind_create(self->root);

}
static void setting_unbind_on_view_will_appear(page_base_t* self)
{


}
static void setting_unbind_on_view_did_appear(page_base_t* self)
{


}
static void setting_unbind_on_view_will_disappear(page_base_t* self)
{


}
static void setting_unbind_on_view_did_disappear(page_base_t* self)
{


}
static void setting_unbind_on_view_did_unload(page_base_t* self)
{
    ui_setting_unbind_destroy(self->root);

}
static page_vtable_t setting_unbind_param = {
    .on_custom_attr_config  = setting_unbind_on_custom_attr_config,
    .on_view_load           = setting_unbind_on_view_load,
    .on_view_did_load       = setting_unbind_on_view_did_load,
    .on_view_will_appear    = setting_unbind_on_view_will_appear,
    .on_view_did_appear     = setting_unbind_on_view_did_appear,
    .on_view_will_disappear = setting_unbind_on_view_will_disappear,
    .on_view_did_disappear  = setting_unbind_on_view_did_disappear,
    .on_view_did_unload     = setting_unbind_on_view_did_unload,
};


page_vtable_t* lv_page_setting_unbind_create(void)
{
    return &setting_unbind_param;
}
